import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/user/home.vue'
import About from '../views/user/about.vue'
import UserLogin from '../views/user/userLogin.vue';
import Consult from '../views/user/consult.vue';
import Individualiz from '../views/user/individualiz.vue';
import TeaAbout from '../views/teacher/tea-about.vue';
import Personal from '../views/user/personal.vue';
import PersonalInformation from '../views/personalInformation.vue';
import AccountInformation from '../views/accountInformation.vue';
import Consulting from '../views/consulting.vue';
import MyMessage from '../views/myMessage.vue';
import OrderRecord from '../views/orderRecord.vue';
import RealName from '../views/realName.vue';
import Wallet from '../views/wallet.vue';
import Comment from '../views/comment.vue';
import Message from '../views/message.vue';
import Index from '@/views/index.vue';
import store from "@/store";
import {Tool} from "@/util/tool";

const routes: Array<RouteRecordRaw> = [
  {
    path: '/index',
    name: 'Index',
    component: Index,
    children: [
      {
        path: '/',
        name: 'Home',
        component: Home
      },
      {
        path: '/about',
        name: 'About',
        component: About
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        // component: () => import(/* webpackChunkName: "about" */ '../views/about.vue')
      },
      {
        path: '/userLogin',
        name: 'UserLogin',
        component: UserLogin
      },
      {
        path: '/consult',
        name: 'Consult',
        component: Consult
      },
      {
        path: '/individualiz',
        name: 'Individualiz',
        component: Individualiz,
        meta: {
          loginRequire: true
        }
      },
      {
        path: '/personal',
        name: 'Personal',
        component: Personal,
        children: [
          {
            path: 'personalInformation',
            name: 'PersonalInformation',
            component: PersonalInformation,
          },
          {
            path: 'accountInformation',
            name: 'AccountInformation',
            component: AccountInformation,
          },
          {
            path: 'consulting',
            name: 'Consulting',
            component: Consulting,
          },
          {
            path: 'myMessage',
            name: 'MyMessage',
            component: MyMessage,
            children: [
              {
                path: 'comment',
                name: 'Comment',
                component: Comment,
              },
              {
                path: 'message',
                name: 'Message',
                component: Message,
              },
            ],
          },
          {
            path: 'orderRecord',
            name: 'OrderRecord',
            component: OrderRecord,
          },
          {
            path: 'realName',
            name: 'RealName',
            component: RealName,
          },
          {
            path: 'wallet',
            name: 'Wallet',
            component: Wallet,
          },
        ],
        meta: {
          loginRequire: true
        }
      },

      {
        path: '/teaAbout',
        name: 'TeaAbout',
        component: TeaAbout,
        meta: {
          loginRequire: true
        }
      },
    ]
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

// 路由登录拦截
// router.beforeEach((to, from, next) => {
//   // 要不要对meta.loginRequire属性做监控拦截
//   if (to.matched.some(function (item) {
//     console.log(item, "是否需要登录校验：", item.meta.loginRequire);
//     return item.meta.loginRequire;
//   })) {
//     const loginUser = store.state.user;
//     if (Tool.isEmpty(loginUser)) {
//       console.log("用户未登录！");
//       next('/');
//     } else {
//       next();
//     }
//   } else {
//     next();
//   }
// });

export default router
